package vn.edu.hut.soict.translate.wordgraph;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import vn.edu.hut.soict.automaton.Transition;

public class WordGraphReader {
	private WordGraph[][] graphs;
	private WordGraph currentGraph;
	
	public WordGraph[][] read(String file) {
		BufferedReader reader = null;
		graphs = null;
		currentGraph = null;
		SAXBuilder sax = new SAXBuilder();
		Document doc;
		
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
			doc = sax.build(reader);
			Element root = doc.getRootElement();
			readRoot(root);			
			for (Object wordgraph : root.getChildren()) {
				readWordGraph((Element) wordgraph);
			}
		}
		catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return graphs;
	}
	
	private void readRoot(Element root) {
		int maxCW = Integer.parseInt(root.getAttributeValue("maxCW"));
		int maxFW = Integer.parseInt(root.getAttributeValue("maxFW"));
		graphs = new WordGraph[maxCW][maxFW];
		for (int i = 0; i < maxCW; i++) {
			for (int j = 0; j < maxFW; j++) {
				graphs[i][j] = null;
			}
		}
	}
	
	private void readWordGraph(Element g) {
		int cW = Integer.parseInt(g.getAttributeValue("cW"));
		int fW = Integer.parseInt(g.getAttributeValue("fW"));
		WordGraph graph = new WordGraph();
		graph.setContentWordCount(cW);
		graph.setFunctionWordCount(fW);
		currentGraph = graph;
		for (Object c : g.getChildren()) {
			Element e = (Element) c;
			if (e.getName().equals("states")) {
				for (Object s : e.getChildren()) {
					readState((Element) s);
				}
			}
			else if (e.getName().equals("transitions")) {
				for (Object t : e.getChildren()) {
					readTransition((Element) t);
				}
			}
		}
		graphs[cW][fW] = graph;
	}
	
	private void readState(Element s) {
		int id = Integer.parseInt(s.getText());
		if (id != 0 && id != 1) {
			currentGraph.addNewState();
		}
	}
	
	private void readTransition(Element t) {
		String text = null;
		String pos = null;
		int source = -1;
		int target = -1;
		for (Object c : t.getChildren()) {
			Element e = (Element) c;
			if (e.getName().equals("symbol")) {
				text = e.getText();
				pos = e.getAttributeValue("pos");
			}
			else if (e.getName().equals("source")) {
				source = Integer.parseInt(e.getText());
			}
			else if (e.getName().equals("target")) {
				target = Integer.parseInt(e.getText());
			}
		}
		currentGraph.addTransition(new Transition<Word>(currentGraph.getStateByID(source), currentGraph.getStateByID(target), new DefaultWord(text, pos)));
	}
}
